﻿using KZ.Express.ENT;
using KZ.Express.Presenter;
using KZ.Express.Resource;
using KZ.Utilities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace KZ.Express.Order.Log
{
    public partial class LogDetailView : FormBase
    {
        #region Private Methods

        private void LogDetailView_Load(object sender, EventArgs e)
        {
            try
            {
                _presenter.LoadEntity();
            }
            catch (BusinessLogicException ex)
            {
                SetMessage(ex.Message, false);
            }
        }

        #endregion

        #region Constructors

        public LogDetailView()
        {
            InitializeComponent();
            base.ObjectID = (int)Objects.Account;
            base._presenter = new PresenterBase(this, new LogENT());

            base.Action = ActionName.View;
        }

        #endregion

        #region Public Properties

        public override EntityBase Entity
        {
            set
            {
                LogENT log = value as LogENT;
                txtDate.Text = log.Date.Value.ToShortPattern(Utility.Info);
                txtEvent.Text = log.Event;
                txtFullName.Text = log.FullName;

                XmlDocument xmlDoc = new XmlDocument();
                if (!String.IsNullOrWhiteSpace(log.OldContent))
                {
                    log.OldContent = log.OldContent
                       .Replace("<Order>", "<DonHang>").Replace("</Order>", "</DonHang>")
                       .Replace("<OrderNumber>", "<SoBill>").Replace("</OrderNumber>", "</SoBill>")
                       .Replace("<OrderDate>", "<Ngay>").Replace("</OrderDate>", "</Ngay>")
                       .Replace("<CarrierName>", "<DichVu>").Replace("</CarrierName>", "</DichVu>")
                       .Replace("<Account>", "<Account>").Replace("</Account>", "</Account>")
                       .Replace("<Customer>", "<KhachHang>").Replace("</Customer>", "</KhachHang>")
                       .Replace("<Country>", "<QuocGia>").Replace("</Country>", "</QuocGia>")
                       .Replace("<Type>", "<Loai>").Replace("</Type>", "</Loai>")
                       .Replace("<OriginalPriceList>", "<BangGiaGoc>").Replace("</OriginalPriceList>", "</BangGiaGoc>")
                       .Replace("<CustomerPriceList>", "<BangGiaKH>").Replace("</CustomerPriceList>", "</BangGiaKH>")
                       .Replace("<Weight>", "<KhoiLuong>").Replace("</Weight>", "</KhoiLuong>")
                       .Replace("<StandardPrice>", "<GiaGocUSD>").Replace("</StandardPrice>", "</GiaGocUSD>")
                       .Replace("<ListPrice>", "<GiaBanUSD>").Replace("</ListPrice>", "</GiaBanUSD>")
                       .Replace("<OriFuel>", "<XangDauGiaGoc>").Replace("</OriFuel>", "</XangDauGiaGoc>")
                       .Replace("<Fuel>", "<XangDauGiaBan>").Replace("</Fuel>", "</XangDauGiaBan>")
                       .Replace("<OriVAT>", "<VATGiaGoc>").Replace("</OriVAT>", "</VATGiaGoc>")
                       .Replace("<VAT>", "<VATGiaBan>").Replace("</VAT>", "</VATGiaBan>")
                       .Replace("<Cost>", "<PhuPhi>").Replace("</Cost>", "</PhuPhi>")
                       .Replace("<VNDListOptional>", "<DieuChinh>").Replace("</VNDListOptional>", "</DieuChinh>")
                       .Replace("<Rate>", "<TiGia>").Replace("</Rate>", "</TiGia>")
                       .Replace("<VNDStandardPrice>", "<GiaGocVND>").Replace("</VNDStandardPrice>", "</GiaGocVND>")
                       .Replace("<VNDListPrice>", "<GiaBanVND>").Replace("</VNDListPrice>", "</GiaBanVND>")
                       .Replace("<Description>", "<GhiChu>").Replace("</Description>", "</GhiChu>");

                    xmlDoc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + log.OldContent);

                    using (XmlTextWriter xtw = new XmlTextWriter("old.xml", Encoding.UTF8))
                    {
                        xmlDoc.Save(xtw);
                    }

                    wbbOld.Navigate(Application.StartupPath + @"\old.xml");
                }

                if (!String.IsNullOrWhiteSpace(log.NewContent))
                {
                    log.NewContent = log.NewContent
                       .Replace("<Order>", "<DonHang>").Replace("</Order>", "</DonHang>")
                       .Replace("<OrderNumber>", "<SoBill>").Replace("</OrderNumber>", "</SoBill>")
                       .Replace("<OrderDate>", "<Ngay>").Replace("</OrderDate>", "</Ngay>")
                       .Replace("<CarrierName>", "<DichVu>").Replace("</CarrierName>", "</DichVu>")
                       .Replace("<Account>", "<Account>").Replace("</Account>", "</Account>")
                       .Replace("<Customer>", "<KhachHang>").Replace("</Customer>", "</KhachHang>")
                       .Replace("<Country>", "<QuocGia>").Replace("</Country>", "</QuocGia>")
                       .Replace("<Type>", "<Loai>").Replace("</Type>", "</Loai>")
                       .Replace("<OriginalPriceList>", "<BangGiaGoc>").Replace("</OriginalPriceList>", "</BangGiaGoc>")
                       .Replace("<CustomerPriceList>", "<BangGiaKH>").Replace("</CustomerPriceList>", "</BangGiaKH>")
                       .Replace("<Weight>", "<KhoiLuong>").Replace("</Weight>", "</KhoiLuong>")
                       .Replace("<StandardPrice>", "<GiaGocUSD>").Replace("</StandardPrice>", "</GiaGocUSD>")
                       .Replace("<ListPrice>", "<GiaBanUSD>").Replace("</ListPrice>", "</GiaBanUSD>")
                       .Replace("<OriFuel>", "<XangDauGiaGoc>").Replace("</OriFuel>", "</XangDauGiaGoc>")
                       .Replace("<Fuel>", "<XangDauGiaBan>").Replace("</Fuel>", "</XangDauGiaBan>")
                       .Replace("<OriVAT>", "<VATGiaGoc>").Replace("</OriVAT>", "</VATGiaGoc>")
                       .Replace("<VAT>", "<VATGiaBan>").Replace("</VAT>", "</VATGiaBan>")
                       .Replace("<Cost>", "<PhuPhi>").Replace("</Cost>", "</PhuPhi>")
                       .Replace("<VNDListOptional>", "<DieuChinh>").Replace("</VNDListOptional>", "</DieuChinh>")
                       .Replace("<Rate>", "<TiGia>").Replace("</Rate>", "</TiGia>")
                       .Replace("<VNDStandardPrice>", "<GiaGocVND>").Replace("</VNDStandardPrice>", "</GiaGocVND>")
                       .Replace("<VNDListPrice>", "<GiaBanVND>").Replace("</VNDListPrice>", "</GiaBanVND>")
                       .Replace("<Description>", "<GhiChu>").Replace("</Description>", "</GhiChu>");

                    xmlDoc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + log.NewContent);

                    using (XmlTextWriter xtw = new XmlTextWriter("new.xml", Encoding.UTF8))
                    {
                        xmlDoc.Save(xtw);
                    }

                    wbbNew.Navigate(Application.StartupPath + @"\new.xml");
                }
            }
        }

        #endregion Public Properties
    }
}
